package cn.com.fssg.platform.base.handler.impl.gsl.lz;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 工商联履职信息查询数据接口
 */
@Component
public class GslLzSelectCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String id = jsonObject.getString("id");
        if(StringUtils.isNotEmpty(id)) {
            String sql = "select * from gsl_assessment where id=:id";
            Map<String, Object> param = new HashMap<>();
            param.put("id", jsonObject.getString("id"));
            List<Map<String, Object> > accList = jdbcTemplate.queryForList(sql, param);
            if(!accList.isEmpty()) {
                Map<String, Object> acc = accList.get(0);
                //获取考核项信息
                String detailSql = "select * from gsl_assessment_subject where del_flag = '0' and assessment_id=:assessment_id";
                Map<String, Object> detailParam = new HashMap<>();
                detailParam.put("assessment_id", jsonObject.getString("id"));
                List<Map<String, Object>> accDetailList = jdbcTemplate.queryForList(detailSql, detailParam);
                List<Map<String, Object>> resultList = new ArrayList<>();
                accDetailList.forEach(it -> {
                    Map<String, Object> detail = convertKeysToCamelCase(it);
                    resultList.add( detail);
                });
                acc.put("assList", resultList);
                return acc;
            }
        }
        return new HashMap<>();
    }

    public static Map<String, Object> convertKeysToCamelCase(Map<String, Object> map) {
        if (map == null) {
            return new HashMap<>();
        }

        Map<String, Object> result = new HashMap<>();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            // 安全处理空键
            if (key == null) {
                continue; // 跳过空键
            }

            String camelKey = underscoreToCamelCase(key);
            Object convertedValue = convertValue(value);

            result.put(camelKey, convertedValue);
        }

        return result;
    }

    private static Object convertValue(Object value) {
        if (value == null) {
            return null;
        }

        // 处理嵌套 Map
        if (value instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> nestedMap = (Map<String, Object>) value;
            return convertKeysToCamelCase(nestedMap);
        }

        // 处理其他可能需要转换的类型
        return value;
    }

    private static String underscoreToCamelCase(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        StringBuilder result = new StringBuilder();
        boolean capitalizeNext = false;
        boolean firstChar = true;

        for (char c : input.toCharArray()) {
            if (c == '_') {
                capitalizeNext = true;
            } else {
                if (capitalizeNext) {
                    result.append(Character.toUpperCase(c));
                    capitalizeNext = false;
                } else {
                    // 首字母小写处理
                    if (firstChar) {
                        result.append(Character.toLowerCase(c));
                        firstChar = false;
                    } else {
                        result.append(c);
                    }
                }
            }
        }

        return result.toString();
    }
}